package jmars.mobile.worker.gephi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import jmars.mobile.common.Investigator;
import jmars.mobile.datainvestigation.Summary;

/**
 * 
 * @author Jan
 *
 */
public class GPRSLACClusterEdges implements Investigator {

	private String getSubnetAddress(String ip){
		return ip.substring(0, ip.lastIndexOf("."));
	}
	@Override
	public Long compute(File[] files, FileWriter writer) throws IOException {
		Long total = 0L;
		FileInputStream fis;
		BufferedReader reader;
		String row;
		String[] cols;
		Map<String,Set<String>> lacSGSN=new HashMap<String,Set<String>>();
		Map<Set<String>,Set<String>> lacClusters=new HashMap<Set<String>,Set<String>>();
		Map<Set<String>,Integer> edgeCount=new HashMap<Set<String>,Integer>();
		Map<Set<String>,Double> edgeTraffic=new HashMap<Set<String>,Double>();
		Set<String> sgsns;
		Set<String> edgeKey;
		DecimalFormat df = new DecimalFormat();
		df.applyPattern("0");
		for(File inputFile:files){
			fis = new FileInputStream(inputFile);
			reader = new BufferedReader(new InputStreamReader(fis));
			while((row=reader.readLine())!=null){
				try{
				cols=row.split(Summary.columnSeperator);
				if(Summary.filterRow(cols,false,false)) continue;
				if(cols.length<20)continue;
				if(cols[18].indexOf(".")==-1)continue;
				if(!lacSGSN.containsKey(cols[1])){
					sgsns = new HashSet<String>();
					sgsns.add(getSubnetAddress(cols[18]));
					lacSGSN.put(cols[1], sgsns);
				}else{
					sgsns = lacSGSN.get(cols[1]);
					sgsns.add(getSubnetAddress(cols[18]));
				}
				edgeKey=new HashSet<String>();
				edgeKey.add(cols[1]);
				edgeKey.add(getSubnetAddress(cols[18]));
				if(!edgeCount.containsKey(edgeKey)){
					edgeCount.put(edgeKey, 1);
					edgeTraffic.put(edgeKey, Double.parseDouble(cols[10]));
				}else{
					edgeTraffic.put(edgeKey, edgeTraffic.get(edgeKey)+Double.parseDouble(cols[10]));
				}
				total++;
				}catch(Exception e){
					continue;
				}
			}
			reader.close();
			fis.close();
			System.out.println(inputFile.getName()+" finished at "+total);
		}
		for(String lac:lacSGSN.keySet()){
			if(lacClusters.containsKey(lacSGSN.get(lac))){
				lacClusters.get(lacSGSN.get(lac)).add(lac);
			}else{
				sgsns=new HashSet<String>();
				sgsns.add(lac);
				lacClusters.put(lacSGSN.get(lac), sgsns);
			}
		}
		System.out.print("%total:"+df.format(total)+"\r\n");
        System.out.print("%*****************details******************\r\n%");
		writer.write("Source,Target,Type,Id,Label,Count,traffic_total\r\n");
		int t=0;
		int count=0;
		Double tt=0D;
		for(Set<String> s:lacClusters.keySet()){
			t++;
			for(String s2:s){
				count=0;tt=0D;
				for(String s3:lacClusters.get(s)){
					edgeKey=new HashSet<String>();
					edgeKey.add(s3);
					edgeKey.add(s2);
					if(edgeCount.containsKey(edgeKey))count+=edgeCount.get(edgeKey);
					if(edgeTraffic.containsKey(edgeKey))tt+=edgeTraffic.get(edgeKey);
				}
				writer.write("LACC#"+t+","+s2+",Directed,LACC"+t+s2+","+count+","+tt+"\r\n");
			}
		}
		return total;
	}

}
